//
//  HomeTestingViewController.m
//  ZJControlsClass
//
//  Created by mac on 2024/11/15.
//

#import "HomeTestingViewController.h"


#import "HomeAvailableNetworking.h"
#import "HomeTestingTableViewCell.h"
#import "HomeAvailableModel.h"
#import "publicNodataView.h"






@interface HomeTestingViewController ()<UITableViewDelegate,UITableViewDataSource,protocolReloadTestingTableViewDelegate>

@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) HomeLookTestingNetWorking * manager;

@property (nonatomic,strong) publicNodataView * nodataView;


@end

@implementation HomeTestingViewController



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self loadNet];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = COLOR_RGB(241, 241, 241);
    
    [self setUpUI];
    [self loadNet];
    
    
}

-(void)setUpUI{
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.nodataView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    [self.nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
}


-(void)loadNet{
    
    [self.manager dispatchTaskWithParam:@{} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] intValue] == 200){
            [self.dataSource removeAllObjects];
            [self.dataSource addObjectsFromArray:[HomeAvailableModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]]];
            if (self.dataSource.count == 0) {
                self.nodataView.hidden = NO;
            }else{
                self.nodataView.hidden = YES;
            }
            [self.tableView reloadData];
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                
            }];
        }
        
    }];
    
    
    
}


#pragma mark -- protocolReloadTestingTableViewDelegate
-(void)realodaTestingTableView{
    [self loadNet];
}




#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTestingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTestingTableViewCell"];
    if (!cell){
        cell = [[HomeTestingTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeTestingTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSource[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(220);
}





#pragma mark -- 懒加载
-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(HomeLookTestingNetWorking *)manager{
    if (!_manager){
        _manager = [HomeLookTestingNetWorking new];
    }
    return _manager;
}

-(publicNodataView *)nodataView{
    if (!_nodataView) {
        _nodataView = [publicNodataView new];
        _nodataView.hidden = YES;
    }
    return _nodataView;
}

@end
